<template>
  <div>
    <webview src="../../../static/print.html" nodeintegration></webview>
  </div>
</template>

<script>
const ipcRenderer = require("electron").ipcRenderer;
export default {
  name: "landing-page",
  components: {},
  props: ["printData"],
  data() {
    return {
      printer: "",
      webview: null
    };
  },
  async mounted() {
    await this.getPrinterList(); //首先先获取
    this.initPrintEvent();
  },
  methods: {
    initPrintEvent() {
      this.webview = document.querySelector("webview");
      this.webview.addEventListener("dom-ready", () => {
        console.log(this.printData);
        this.webview.send("ping", this.printData);
      });
      this.webview.addEventListener("ipc-message", event => {
        if (event.channel == "pong") {
          console.log("通信成功");
          this.webview.print(
            {
              silent: true,
              printBackground: true,
              deviceName: this.printer
            },
            data => {
              this.$emit("printCompelete");
            }
          );
        }
      });
    },
    getPrinterList() {
      ipcRenderer.send("getPrinterList");
      //监听主线程获取到打印机列表后的回调
      ipcRenderer.once("getPrinterList", (event, data) => {
        //data就是打印机列表
        this.printer = data[0].name;
      });
    }
  }
};
</script>

<style>
</style>
